
#include "interf.h"

#include "FHE.h"
#include "EncryptedArray.h"
long w2 = 64;                // Hamming weight of secret key

FHEcontext *context2;
EncryptedArray *ea2;
FHESecKey *secretKey2;
string strRe2;
bool isinit2 = false;
void init2(){
    long m = 2, p = 715827883, r = 2;   // Native plaintext space(本机明文空间)
    long L = 16;                // Levels
    long c = 3;                 // Columns in key switching matrix
    context2 = new FHEcontext(m, p, r);    // 初始化上下文, 《非常慢》
    buildModChain(*context2, L, c);   //修改上下文，增加模链的素数
    ZZX G = context2->alMod.getFactorsOverZZ()[0];
    ea2 = new EncryptedArray(*context2, G);  //constuct是一个加密的数组对象ea , 与给定的上下文和多项式G相关联
    secretKey2 = new FHESecKey(*context2);   // 构造与上下文相关的秘密关键结构
    //  publicKey = new FHESecKey(*context);
    // publicKey = secretKey; // FHESecKey是FHEPubKey的一个子类
    secretKey2->GenSecKey(w2);  //实际上生成的秘钥
    addSome1DMatrices(*secretKey2); //计算我们需要的键交换矩阵
    isinit2 = true;
}

void test0() {
    if (!isinit2) init2();
    //-------------------------------------------------------------------------------
    const FHEPubKey &publicKey2 = *secretKey2; // FHESecKey是FHEPubKey的一个子类
    Ctxt ct2_p(publicKey2);
    vector<long> v2;
    v2.push_back((1 + 3) * 233);  //2460877642

    printf("---------------加密前-----------------\n");
    cout << "encrypt v2  :" << v2 << endl;
    ea2->encrypt(ct2_p, publicKey2, v2); //plaintxt values v2, ciphertxt values ct2

    /*   printf("---------------密文-----------------\n");
       // 密文
        cout << "encrypt ct2 " << ct2_p << endl;
   */
    printf("-------------解密-------------------\n");
    vector<long> v2_j;
    ea2->decrypt(ct2_p, *secretKey2, v2_j);
    cout << "decrypt v2_j  :" << v2_j << endl;

    //2
    /*  NewPlaintextArray p11(*ea);
      encode(*ea, p11, text);
      ea->encrypt(ct2_p, publicKey, p11);
  */
}